﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;


namespace Web.Page
{
    public partial class ZL_CK_QueryTest :BasePage
    {

        ZL_CK_Dao dao = new ZL_CK_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                this.lbTitle.Text = "仓库列表";
                initValue();
                BindGridview();
            }
        }

        private void initValue()
        {
            CS_CKLX_Dao daoCKLX = new CS_CKLX_Dao();
            List<CS_CKLX> lsCPLX = daoCKLX.QueryAll();
            this.ddlCKLXBH.DataSource = lsCPLX;
            this.ddlCKLXBH.DataValueField = "BH";
            this.ddlCKLXBH.DataTextField = "MC";
            this.ddlCKLXBH.DataBind();
            this.ddlCKLXBH.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        private void BindGridview()
        {
            GridView1.PageIndex = RestorePageIndex();
            List<ZL_CK> list = query(); // dao.QueryAll();
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    ((ImageButton)(e.Row.Cells[6].Controls[0])).Attributes.Add("onclick ", "return   confirm( '确定删除？ ') ");
            //}
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            int result = dao.Delete(id);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
            }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/ZL_CK_Add.aspx");
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            BindGridview();

            //string bh = this.tbBH.Text;
            //string mc = this.tbMC.Text;
            //string lxbh = this.ddlCKLXBH.SelectedValue;

            //List<ZL_CK> list = dao.QueryBySql(bh, mc, lxbh);
            //this.GridView1.DataSource = list;
            //this.GridView1.DataBind();
        }

        protected List<ZL_CK> query()
        {
            string bh = this.tbBH.Text;
            string mc = this.tbMC.Text;
            string lxbh = this.ddlCKLXBH.SelectedValue;

            return dao.QueryBySql(bh, mc, lxbh);
        }
    }
}